package com.zmx.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

/**

 */
@Configuration //spring配置文件
public class QuartzConfig {

    @Bean  //<bean class="">
    public SchedulerFactoryBean schedulerFactoryBean
            (@Qualifier("dataSource")DataSource dataSource){
        //定时任务 持久化保存到数据库  所以需要dataSource
        SchedulerFactoryBean factoryBean  = new SchedulerFactoryBean();
        factoryBean.setDataSource(dataSource);
        //quartz配置
        Properties properties = new Properties();
        properties.setProperty("org.quartz.scheduler.instanceName","MyQuartzScheduler");
        properties.setProperty("org.quartz.scheduler.instanceId","AUTO");
        properties.setProperty("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
        properties.setProperty("org.quartz.threadPool.threadCount","10");
        //持久化到数据库
        properties.setProperty("org.quartz.jobStore.class","org.quartz.impl.jdbcjobstore.JobStoreTX");
        properties.setProperty(" org.quartz.jobStore.tablePrefix","QRTZ_");
        factoryBean.setQuartzProperties(properties);

        factoryBean.setStartupDelay(5);
        factoryBean.setAutoStartup(true);
        factoryBean.setOverwriteExistingJobs(true);
        return  factoryBean;
    }
}
